package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.example.buildtable.pojo.ColumnMenu;
import com.example.buildtable.pojo.Goods;
import com.example.common.util.R;
import com.example.buildtable.pojo.GoodsRecycle;
import com.example.buildtable.service.GoodsRecycleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "商品回收站")
@RequestMapping(value = "/goodsRecycle/")
public class GoodsRecycleController {
    @Autowired
    private GoodsRecycleService goodsRecycleService;
    
    @Operation(summary = "查询所有商品回收站数据")
    @GetMapping(value = "/getAllGoodsRecycle")
    public R getAllGoodsRecycle() {
        LambdaJoinQueryWrapper<GoodsRecycle> wrapper = new LambdaJoinQueryWrapper<>(GoodsRecycle.class);
        wrapper.innerJoin(Goods.class, Goods::getGoodsId, GoodsRecycle::getGoodsRecycleId);
        List<GoodsRecycle> goodsRecycleList = goodsRecycleService.list(wrapper);
        return R.ok("查询所有商品回收站数据成功").data("goodsRecycleList", goodsRecycleList);
    }
    
    @Operation(summary = "增加商品回收站")
    @PostMapping(value = "/addGoodsRecycle")
    public R addGoodsRecycle(GoodsRecycle goodsRecycle) {
        boolean flag = goodsRecycleService.save(goodsRecycle);
        if (flag) {
            return R.ok("增加商品回收站成功");
        } else {
            return R.ok("增加商品回收站失败");
        }
    }

    @Operation(summary = "删除商品回收站")
    @GetMapping(value = "/deleteGoodsRecycle/{goodsRecycleId}")
    public R deleteGoodsRecycle(@PathVariable("goodsRecycleId") Integer goodsRecycleId) {
        boolean flag = goodsRecycleService.removeById(goodsRecycleId);
        if (flag) {
            return R.ok("删除商品回收站成功");
        } else {
            return R.ok("删除商品回收站失败");
        }
    }

    @Operation(summary = "按照商品回收站查看")
    @GetMapping(value = "/getGoodsRecycleById/{goodsRecycleId}")
    public R getGoodsRecycleById(@PathVariable("goodsRecycleId") Integer goodsRecycleId) {
        LambdaJoinQueryWrapper<GoodsRecycle> wrapper = new LambdaJoinQueryWrapper<>(GoodsRecycle.class);
        wrapper.innerJoin(Goods.class, Goods::getGoodsId, GoodsRecycle::getGoodsRecycleId);
        wrapper.eq(GoodsRecycle::getGoodsRecycleId, goodsRecycleId);
        GoodsRecycle goodsRecycle = goodsRecycleService.getOne(wrapper);
        if (goodsRecycle != null) {
            return R.ok("按照商品回收站查看成功").data("goodsRecycle", goodsRecycle);
        } else {
            return R.error("按照商品回收站查看失败");
        }
    }

    @Operation(summary = "修改商品回收站")
    @PostMapping(value = "/updateGoodsRecycle")
    public R updateGoodsRecycle(GoodsRecycle goodsRecycle) {
        boolean flag = goodsRecycleService.updateById(goodsRecycle);
        if (flag) {
            return R.ok("修改商品回收站成功");
        } else {
            return R.ok("修改商品回收站失败");
        }
    }
}